import axios from 'axios'
import router from '../router'
import { Loading, Message } from 'element-ui'
let instence = axios.create()
instence.defaults.timeout = 5000
let loadinginstace
instence.interceptors.request.use(config => {
    loadinginstace = Loading.service({ fullscreen: true })
    config.headers.Authorization = localStorage.getItem('token') || ''
    return config
}, error => {
    loadinginstace.close()
    Message.error('加载超时')
    return Promise.reject(error)
})
instence.interceptors.response.use(data => {
    loadinginstace.close()
    if (data.status === 200) {
        return data
    }
    if (data.status === 405) {
        Message.error('登录过期，请重新登录')
        router.push('/login')
    }
}, error => {
    loadinginstace.close()
    return Promise.reject(error)
})
export default instence
